/*
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-12 14:58:47
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-12 15:41:00
 * @FilePath: /webServer/src/controller/file/file.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Body,
  Controller,
  Del,
  Files,
  Get,
  Inject,
  Post,
  Put,
  Query,
} from '@midwayjs/core';
import { FileService } from '../../service/file/file.service';
import { Validate } from '@midwayjs/validate';
import { ListParamsValidator, StrIdValidator } from '../../validator';
import {
  EditFileValidator,
  SaveFileValidator,
} from '../../validator/file/file';
import { Context } from '@midwayjs/koa';
import { UserService } from '../../service/user/user.service';
import { isNotEmpty } from '../../utils/isHelper';
import { ForbiddenError } from '@midwayjs/core/dist/error/http';
import { FileSchema, UploadFile } from '../../schema';

@Controller('/file', { tagName: '' })
export class FileController {
  @Inject()
  fileService: FileService;

  @Inject()
  userService: UserService;

  @Inject()
  ctx: Context;

  @Post('/list', { summary: '列表' })
  @Validate()
  async list(@Body() body: ListParamsValidator<FileSchema>) {
    const data = await this.fileService.list(body);
    return Object.assign(data, body);
  }

  @Post('/save', { summary: '保存' })
  @Validate()
  async save(@Body() body: SaveFileValidator) {
    return await this.fileService.save(body);
  }

  @Get('/info', { summary: '信息' })
  @Validate()
  async info(@Query() body: StrIdValidator) {
    return await this.fileService.info(body.id);
  }

  @Del('/remove', { summary: '删除' })
  @Validate()
  async remove(@Query() body: StrIdValidator) {
    return await this.fileService.remove(body.id);
  }

  @Del('/delete', { summary: '（真）删除' })
  @Validate()
  async realRemove(@Query() body: StrIdValidator) {
    return await this.fileService.realRemove(body.id);
  }

  @Put('/edit', { summary: '编辑' })
  @Validate()
  async edit(@Query() query: StrIdValidator, @Body() body: EditFileValidator) {
    const file = await this.fileService.info(query.id);
    Object.assign(file, body);
    return await this.fileService.save(file);
  }

  @Post('/uploads', { summary: '文件批量上传' })
  @Validate()
  async upload(@Files() files: Array<UploadFile>) {
    const { uid } = this.ctx.header;
    if (isNotEmpty(uid)) {
      const user = await this.userService.info(uid);
      if (!isNotEmpty(user)) {
        throw new ForbiddenError('无权访问');
      }
      files = files.map(ele =>
        Object.assign(ele, {
          createUserName: user.userName,
          createUserId: user.id,
        })
      );
    }
    const url = filePath => {
      const paths = filePath.split('/');
      return paths[paths.length - 1];
    };
    const params = files.map(file => {
      return {
        fileName: file.filename,
        mimeType: file.mimeType,
        fileUrl: `/uploads/${url(file.data)}`,
        createUserName: file.createUserName,
        createUserId: file.createUserId,
      };
    });

    return await this.fileService.save(params);
  }
}
